package com.wskj.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.system.bo.SysContactUsAddBo;
import com.wskj.system.bo.SysContactUsEditBo;
import com.wskj.system.bo.SysContactUsQueryBo;
import com.wskj.system.domain.SysContactUs;
import com.wskj.system.mapper.SysContactUsMapper;
import com.wskj.system.service.ISysContactUsService;
import com.wskj.system.vo.web.SysContactUsVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 联系我们Service业务层处理
 *
 * @author wsdz
 * @date 2022-12-13
 */
@Service
public class SysContactUsServiceImpl extends ServicePlusImpl<SysContactUsMapper, SysContactUs> implements ISysContactUsService {

    @Override
    public SysContactUsVo queryById(Long usId){
        return getVoById(usId, SysContactUsVo.class);
    }

    @Override
    public TableDataInfo<SysContactUsVo> queryPageList(SysContactUsQueryBo bo) {
        PagePlus<SysContactUs, SysContactUsVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysContactUsVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysContactUsVo> queryList(SysContactUsQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysContactUsVo.class);
    }

    private LambdaQueryWrapper<SysContactUs> buildQueryWrapper(SysContactUsQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysContactUs> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getMainPic()), SysContactUs::getMainPic, bo.getMainPic());
        lqw.eq(StrUtil.isNotBlank(bo.getQrCode()), SysContactUs::getQrCode, bo.getQrCode());
        lqw.eq(StrUtil.isNotBlank(bo.getWxCode()), SysContactUs::getWxCode, bo.getWxCode());
        lqw.eq(StrUtil.isNotBlank(bo.getTelCode()), SysContactUs::getTelCode, bo.getTelCode());
        lqw.eq(StrUtil.isNotBlank(bo.getWwwCode()), SysContactUs::getWwwCode, bo.getWwwCode());
        lqw.eq(bo.getStatus() != null, SysContactUs::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysContactUsAddBo bo) {
        SysContactUs add = BeanUtil.toBean(bo, SysContactUs.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysContactUsEditBo bo) {
        SysContactUs update = BeanUtil.toBean(bo, SysContactUs.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysContactUs entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importSysContactUs(List<SysContactUsVo> sysContactUsList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(sysContactUsList), "导入数据不能为空");
        int successNum = 0;
        for (SysContactUsVo sysContactUs : sysContactUsList) {
            SysContactUs add = BeanUtil.toBean(sysContactUs, SysContactUs.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", sysContactUsList.size(), successNum);
        if (successNum != sysContactUsList.size()) {
            msg = msg + String.format("失败 %d 条！", sysContactUsList.size() - successNum);
        }
        return msg;
    }
}
